/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controler;

import Model.Dao.DAOKhachHang;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import Model.Pojo.Khachhang;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpSession;

/**
 *
 * @author THANH QUAN
 */
public class ThemSuaKhachHang
        extends HttpServlet
{

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        request.setAttribute("QUAN_LY_KHACH_HANG", "1");
        response.setContentType("text/html; charset=UTF-8");
        request.setCharacterEncoding("UTF8");
        DAOKhachHang daoKH = new DAOKhachHang();
        Khachhang dtoKh = null;

        HttpSession session = request.getSession(true);
        dtoKh = (Khachhang) session.getAttribute("CHI_TIET_KHACH_HANG");

        //Cap nhat thong tin khach hang
        if (dtoKh != null)//Cap nhat khach hang
        {
            try
            {
                dtoKh.setCmnd(request.getParameter("txtCMND"));
                dtoKh.setDiachi(request.getParameter("txtDiaChi"));
                dtoKh.setEmail(request.getParameter("txtEmail"));
                dtoKh.setGhichu(request.getParameter("txtGhiChu"));

                dtoKh.setGioitinh(request.getParameter("chkGioiTinh"));
                Object attribute = request.getAttribute("chkGioiTinh");

                dtoKh.setHoten(request.getParameter("txtHoTen"));
                //dtoKh.setMakh(request.getParameter("txtMaKhachHang"));
                dtoKh.setNgaylap(new Date());
                dtoKh.setNgaysinh(CXuLy.ConvertStringToDate(request.getParameter("txtNgaySinh").
                        replace(
                        '-', '/')));
                dtoKh.setSodienthoai(request.getParameter("txtSoDienThoai"));
                dtoKh.setTaikhoannganhang(request.getParameter("txtTaiKhoanNganHang"));
                dtoKh.setTinhtrang(true);

                daoKH.UpdateKhachHang(dtoKh);
                session.removeAttribute("CHI_TIET_KHACH_HANG");
                response.sendRedirect("danh-sach-khach-hang.htm");
                return;
            }
            catch (Exception ex)
            {
                request.setAttribute("ERROR", "Cập nhập dữ liệu không thành công.");
            }
        }
        else//Insert khách hàng mới
        {
            try
            {
                dtoKh = new Khachhang();

                dtoKh.setCmnd(request.getParameter("txtCMND"));
                dtoKh.setDiachi(request.getParameter("txtDiaChi"));
                dtoKh.setEmail(request.getParameter("txtEmail"));
                dtoKh.setGhichu(request.getParameter("txtGhiChu"));
                dtoKh.setGioitinh(request.getParameter("chkGioiTinh"));
                Object attribute = request.getAttribute("chkGioiTinh");
                dtoKh.setHoten(request.getParameter("txtHoTen"));
                dtoKh.setMakh(request.getParameter("txtMaKhachHang"));
                dtoKh.setNgaylap(new Date());
                dtoKh.setNgaysinh(CXuLy.ConvertStringToDate(request.getParameter("txtNgaySinh").
                        replace(
                        '-', '/')));
                dtoKh.setSodienthoai(request.getParameter("txtSoDienThoai"));
                dtoKh.setTaikhoannganhang(request.getParameter("txtTaiKhoanNganHang"));
                dtoKh.setTinhtrang(true);

                daoKH.InsertKhacHang(dtoKh);
            }
            catch (Exception ex)
            {
                request.setAttribute("ERROR", "Thêm khách hàng không thành công.");
            }
            //nhay sang trang moi
            RequestDispatcher view = request.getRequestDispatcher("them-khach-hang.jsp");
            view.forward(request, response);
        }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        if(!CXuLy.PhanQuyen(request, response))
        {
            return;
        }

        request.setAttribute("QUAN_LY_KHACH_HANG", "1");

        String strid_khachhang = request.getParameter("query");
        String url = request.getServletPath();
        //Cap nhat thong tin khach hang
        if (url.equals("/cap-nhat-thong-tin-khach-hang.htm"))
        {
            request.setAttribute("CAP_NHAT_KHACH_HANG", "1");
            if (strid_khachhang == null)
            {
                request.setAttribute("ERROR", "The page you request was not found.");
            }
            else
            {
                try
                {
                    int id_khachhang = Integer.parseInt(strid_khachhang);

                    //lay thong tin khach hang
                    Khachhang dtoKH = null;
                    DAOKhachHang daoKH = new DAOKhachHang();

                    dtoKH = daoKH.ChiTieKhachHang(id_khachhang);

                    if (dtoKH == null)
                    {
                        request.setAttribute("ERROR", "The page you request was not found.");
                    }
                    else
                    {
                        request.setAttribute("CHI_TIET_KHACH_HANG", dtoKH);
                        HttpSession session = request.getSession(true);
                        session.setAttribute("CHI_TIET_KHACH_HANG", dtoKH);
                    }
                }
                catch (Exception ex)
                {
                    request.setAttribute("ERROR", "The page you request was not found.");
                }
            }
        }
        RequestDispatcher view = request.getRequestDispatcher("them-khach-hang.jsp");
        view.forward(request, response);
    }
//

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        processRequest(request, response);

    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo()
    {
        return "Short description";
    }// </editor-fold>
}
